﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.Auth;
using ManagementApi.Models.System.User;
using ManagementApi.Models.WeiXin;
using ManagementApi.Services.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Cms;
using Modules.Cms.Entitys;
using Modules.System.Role;
using Modules.System.User;
using PmSoft.ApplicationContext;
using PmSoft.Entity;

namespace ManagementApi.Controllers.WeiXin
{
    /// <summary>
    /// 小程序-认证控制器
    /// </summary>
    public class AuthController : ControllerBase
    {
        private readonly UserAuthenticationService authenticationService;
        private readonly IApplicationContext applicationContext;
        private readonly UserService userService;
        private readonly IMapper mapper;
        private readonly RoleService roleService;
        private readonly CmsDealerService dealerService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="authenticationService"></param>
        /// <param name="applicationContext"></param>
        /// <param name="userService"></param>
        /// <param name="mapper"></param>
        /// <param name="roleService"></param>
        public AuthController(UserAuthenticationService authenticationService, IApplicationContext applicationContext, UserService userService, IMapper mapper, RoleService roleService, CmsDealerService dealerService)
        {
            this.authenticationService = authenticationService;
            this.applicationContext = applicationContext;
            this.userService = userService;
            this.mapper = mapper;
            this.roleService = roleService;
            this.dealerService = dealerService;
        }

        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("wx_login")]
        public ApiResult<LoginResult> Login(LoginForm form)
        {
            UserLoginForm userLoginForm = new UserLoginForm
            {
                UserType = UserType.SysUser,
                UserName = form.UserName,
                Password = form.Password,
            };
            var loginResult = authenticationService.SignIn(userLoginForm);
            return ApiResult<LoginResult>.Success(loginResult);

        }

        /// <summary>
        /// 获取当前登录用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("me")]
        [Authorize]
        public ApiResult<UserInfoDto> GetUserLoginInfo()
        {
            SysUser? user = userService.Get(applicationContext.CurrentUser?.Id ?? 0);
            if (user == null)
                return ApiResult<UserInfoDto>.Failure("对不起，您还没有登陆，请先登录");
            UserInfoDto userDto = mapper.Map<UserInfoDto>(user);
            IEnumerable<SysRole> roles = roleService.GetRolesByUserId(user.UserId);
            if (!roles.Any(t => t.Code == "JXS"))
                return ApiResult<UserInfoDto>.Failure("对不起，您不是经销商，不能登录");
            CmsDealer? cmsDealer = dealerService.GetDalerByUserId(user.UserId);
            if (cmsDealer == null)
                return ApiResult<UserInfoDto>.Failure("对不起，您不是经销商，不能登录");
            userDto.DealerName = cmsDealer.Name;
            userDto.Address = cmsDealer.Address;
            return ApiResult<UserInfoDto>.Success(userDto);
        }
    }
}
